package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.vo.JazCoursesChapterAddVo;
import com.jinzhi.jzweb.domain.vo.JazCoursesChapterVo;
import com.jinzhi.jzweb.domain.vo.JazCoursesVo;
import org.apache.commons.collections4.Get;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazCoursesChapterDO;
import com.jinzhi.jzweb.service.JazCoursesChapterService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 课程章节表
 * </pre>
 * <small> 2024-05-14 15:43:28 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazCoursesChapter")
public class JazCoursesChapterController extends BaseController {
	@Autowired
	private JazCoursesChapterService jazCoursesChapterService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazCoursesChapter:jazCoursesChapter")
	String JazCoursesChapter(){
	    return "jzweb/jazCoursesChapter/jazCoursesChapter";
	}
	
//	@ResponseBody
//	@GetMapping("/list")
//	@RequiresPermissions("jzweb:jazCoursesChapter:jazCoursesChapter")
//	public Result<Page<JazCoursesChapterDO>> list(JazCoursesChapterDO jazCoursesChapterDTO){
//		// 查询列表数据
//		Page<JazCoursesChapterDO> page = jazCoursesChapterService.selectPage(getPage(JazCoursesChapterDO.class),
//					jazCoursesChapterService.convertToEntityWrapper().orderBy("id", false));
//		List list = new ArrayList();
//		for (int i = 0; i < page.getRecords().size(); i++) {
//				JazCoursesChapterDO obj = (JazCoursesChapterDO) page.getRecords().get(i);
//			list.add(obj);
//		}
//		page.setRecords(list);
//        return Result.ok(page);
//	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazCoursesChapter:add")
	String add(){
	    return "jzweb/jazCoursesChapter/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazCoursesChapter:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazCoursesChapterDO jazCoursesChapter = jazCoursesChapterService.selectById(id);
		model.addAttribute("jazCoursesChapter", jazCoursesChapter);
	    return "jzweb/jazCoursesChapter/edit";
	}
	
//
//	@ResponseBody
//	@PostMapping("/save")
//	@RequiresPermissions("jzweb:jazCoursesChapter:add")
//	public Result<String> save( JazCoursesChapterDO jazCoursesChapter){
//		jazCoursesChapterService.insert(jazCoursesChapter);
//        return Result.ok();
//	}

	/**
	 * 新增章节
	 * @param jazCoursesChapterVo
	 * @return
	 */
	@ResponseBody
	@PostMapping("/saveCourseChapter")
	public Result<String> saveCourseChapter(@RequestBody JazCoursesChapterAddVo jazCoursesChapterVo){
		jazCoursesChapterService.saveCourseChapter(jazCoursesChapterVo);
		return Result.ok();
	}

	/**
	 * 新增子章节
	 * @param jazCoursesChapterVos
	 * @return
	 */
	@ResponseBody
	@PostMapping("/saveCourseChildChapter")
	public Result<String> saveCourseChildChapter(@RequestBody List<JazCoursesChapterAddVo> jazCoursesChapterVos){
		jazCoursesChapterService.saveCourseChildChapter(jazCoursesChapterVos);
		return Result.ok();
	}

	/**
	 * 查询章节
	 */
	@GetMapping("/selectCourseChapter")
	@ResponseBody
	public Result<List<JazCoursesChapterVo>> selectCourseChapter(@RequestParam(value = "courseId")Long courseId) throws Exception {
		List<JazCoursesChapterVo> voList=jazCoursesChapterService.selectCourseChapter(courseId);
		return Result.ok(voList);
	}

	/**
	 * 根据id查询章节
	 */
	@ResponseBody
	@GetMapping("/selectChildCourse")
	public Result<JazCoursesChapterVo> selectChildCourse(@RequestParam(value = "id")Long id)  {
		JazCoursesChapterVo vo=jazCoursesChapterService.selectChildCourse(id);
		return Result.ok(vo);
	}
//	@Log("修改课程章节表")
//	@ResponseBody
//	@RequestMapping("/update")
//	@RequiresPermissions("jzweb:jazCoursesChapter:edit")
//	public Result<String>  update( JazCoursesChapterDO jazCoursesChapter){
//		boolean update = jazCoursesChapterService.updateById(jazCoursesChapter);
//		return update ? Result.ok() : Result.fail();
//	}
	/**
	 * 编辑章节
	 */
	@PostMapping("/updateCourseChapter")
	@ResponseBody
	public Result<String> updateCourseChapter(@RequestBody JazCoursesChapterVo vo) {
		jazCoursesChapterService.updateCourseChapter(vo);
		return Result.ok("");
	}
	

	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("jzweb:jazCoursesChapter:remove")
	public Result<String>  remove( Long id){
		List<JazCoursesChapterDO> doList = jazCoursesChapterService.selectList(new EntityWrapper<JazCoursesChapterDO>().eq("parent_id", id));
		if (!doList.isEmpty()){
			return Result.fail("该章节下有子章节无法删除！");
		}
		jazCoursesChapterService.deleteById(id);
        return Result.ok();
	}
	

	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazCoursesChapter:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazCoursesChapterService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
